package com.comtime.mybox;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.NumberPicker.OnValueChangeListener;

import com.comtime.entity.MusicInfo;
import com.comtime.smartbox.BaseActivity;
import com.comtime.smartbox.HBApplication;
import com.comtime.smartkilimanjaro.R;
import com.comtime.swdevice.SWDevice;
import com.comtime.utils.MusicPlayer;
import com.comtime.utils.MySharedPreferences;
import com.comtime.view.SelectNameModePopupWindow;

public class AntiLostAlarmManagerActivity extends BaseActivity {

	TextView tv_lost_times, tv_lost_music;
	ToggleButton tb_box_wain_light;
	MySharedPreferences mySharedPreferences;
	HBApplication application;
	String[] values;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_set_anlilost_manager);
		mySharedPreferences = MySharedPreferences.getInstance(this);
		application=(HBApplication) getApplication();
		/*MusicInfo musicInfo = null;
		String path = "android.resource://" + getPackageName()
				+ "/" + R.raw.lost;
		musicInfo = new MusicInfo(path);
		MusicPlayer  musicPlayer=new MusicPlayer(this);
		musicPlayer.playWithPath(path);
		mySharedPreferences.saveLostDeviceWainVoicePath(path);*/
		init();
	}

	void init() {

		values = new String[] { "1", "3", "5" };

		tv_lost_times = (TextView) findViewById(R.id.tv_lost_times);
		tv_lost_times
				.setText(mySharedPreferences.getLostDeviceWainTimes() + "");
		switch (mySharedPreferences.getLostDeviceWainTimes()) {
		case 1:
			bufflostWainNum = 0;
			break;
		case 3:
			bufflostWainNum = 1;
			break;
		case 5:
			bufflostWainNum = 2;
			break;
		default:
			break;
		}

		//tv_lost_music = (TextView) findViewById(R.id.tv_lost_music);

		/*tb_box_wain_light = (ToggleButton) findViewById(R.id.tb_box_wain_light);
		tb_box_wain_light.setChecked(mySharedPreferences.getBoxWainLightIsOn());
		tb_box_wain_light
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton button,
							boolean b) {
						// TODO Auto-generated method stub
						mySharedPreferences.saveBoxWainLightIsOn(b);
					}
				});*/

	}

	public void BackAction(View v) {
		this.finish();
		overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
	}

	public void LostWainTimesAction(View v) {

		showSelectNameModePopupWindow(bufflostWainNum, values,
				getText(R.string.wain_times).toString());

	}

	/*public void LostWainMusicAction(View v) {
		Intent intent = new Intent(this, SelectVoiceActivity.class);
		intent.putExtra("selectType", 2);
		startActivity(intent);
	}*/

	SelectNameModePopupWindow selectNameModePopupWindow;

	void showSelectNameModePopupWindow(int currentrvalue, String[] values,
			String title) {
		selectNameModePopupWindow = new SelectNameModePopupWindow(
				AntiLostAlarmManagerActivity.this, itemsOnClick,
				valueChangeListener, currentrvalue, values, title);
		// 显示窗口
		selectNameModePopupWindow.showAtLocation(
				findViewById(R.id.activity_set_anti_lost_manager),
				Gravity.BOTTOM, 0, 0); // 设置layout在PopupWindow中显示的位置
	}

	OnClickListener itemsOnClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_setting_selectalarmtimes_ok:
				tv_lost_times.setText(values[bufflostWainNum]);

				mySharedPreferences.saveLostDeviceWainTimes(Integer
						.parseInt(values[bufflostWainNum]));
				if (application.swDevicesList.get(0).isConnected()) {
					switch (bufflostWainNum) {
					case 0:
						application.swDevicesList.get(0).sendToDevice(SWDevice.SETTING_DEVICE_ALERT_TINES_1);
						break;
					case 1:
						application.swDevicesList.get(0).sendToDevice(SWDevice.SETTING_DEVICE_ALERT_TINES_3);
						break;
					case 2:
						application.swDevicesList.get(0).sendToDevice(SWDevice.SETTING_DEVICE_ALERT_TINES_5);
						break;
					default:
						break;
					}
				}

				selectNameModePopupWindow.dismiss();
				break;
			case R.id.btn_setting_selectalarmtimes_cancel:
				selectNameModePopupWindow.dismiss();
				break;

			default:
				break;
			}
		}
	};

	int bufflostWainNum = 0;
	OnValueChangeListener valueChangeListener = new OnValueChangeListener() {

		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			// TODO Auto-generated method stub
			if (picker.getId() == R.id.numberPicker_setting_times) {
				bufflostWainNum = newVal;

			}
		}
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		tv_lost_times.setText(values[bufflostWainNum]);
		/*tv_lost_music
				.setText(mySharedPreferences.getLostDeviceWainVoiceTitle());*/
	}

}
